<template>
  <div class="app-container">
    <el-form :inline="true">
      <el-form-item label="行业名称">
        <el-input
          v-model="queryParams.businessName"
          placeholder="请输入行业名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="行业代码">
        <el-input
          v-model="queryParams.businessCode"
          placeholder="请输入行业代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="版本列表">
        <el-select v-model="queryParams.versions" placeholder="请选择版本列表" clearable size="small">
          <el-option
            v-for="dict in businessVersionsList"
            :key="dict"
            :label="dict"
            :value="dict"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd()"
          v-hasPermi="['system:dept:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="industryList"
      row-key="businessId"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      stripe :header-cell-style="{background:'#e1f0ff',color:'#000000','margin-top':'22px','height':'48px'}" :cell-style="{'height':'46px','boder-bottom':'0px','padding':'9px 0px'}"
    >
      <el-table-column label="行业名称">

        <template slot-scope="scope">
          {{scope.row.businessCode+scope.row.businessName}}
        </template>
      </el-table-column>
      <!-- <el-table-column prop="businessCode" label="行业代码"  width="150"></el-table-column> -->
      <el-table-column prop="versions" label="版本"  width="150"></el-table-column>
      <el-table-column prop="sort" label="排序" width="120"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="400">
        <template slot-scope="scope">
          <el-button 
            size="mini" 
            type="text" 
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dept:edit']"
          >修改</el-button>
          <el-button 
            size="mini" 
            type="text" 
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:dept:add']"
          >新增</el-button>
          <el-button
            v-if="scope.row.parentId != 0"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dept:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" :close-on-press-escape="false" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="行业名称" prop="businessName">
              <el-input v-model="form.businessName" placeholder="请输入行业名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="行业代码" prop="businessCode">
              <el-input v-model="form.businessCode" placeholder="请输入行业代码" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="版本" prop="versions">
              <el-select v-model="form.versions" placeholder="请选择版本" clearable size="small">
                <el-option
                  v-for="dict in businessVersionsList"
                  :key="dict"
                  :label="dict"
                  :value="dict"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入排序" maxlength="20" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { businessList, areaDel, businessAdd, businessEdit, businessVersions } from "@/api/system/industry";

export default {
  name: "Dept",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 表格树数据
      industryList: [],
      // 部门部门树选项
      deptOptions: undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        businessName: undefined,
        versions:undefined
      },
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {
        businessName: [
          { required: true, message: "行业名称不能为空", trigger: "blur" }
        ],
        businessCode: [
          { required: true, message: "行业代码不能为空", trigger: "blur" }
        ],
        versions: [
          { required: true, message: "版本不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序号不能为空", trigger: "blur" }
        ]
      },
      // 版本列表
      businessVersionsList: [],
    };
  },
  created() {
    this.getList();
    businessVersions().then(res=>{
      if(res.code == 200){
        this.businessVersionsList = res.data
      }
    })
  },
  methods: {
    /** 查询部门列表 */
    getList() {
      this.loading = true;
      businessList(this.queryParams).then(response => {
        this.industryList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        businessName: '',
        sort: 0,
        businessCode:''
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if (row != undefined) {
        this.form.parentId = row.businessId;
        this.form.sort = row.hasOwnProperty('children')? row.children.length+1:0
      }else{
        this.form.sort = this.industryList.length+1
      }
      this.open = true;
      this.title = "添加行业分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = JSON.parse(JSON.stringify(row))
      this.open = true;
      this.title = "修改行业分类";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.businessId != undefined) {
            businessEdit(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            businessAdd(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.businessName + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return areaDel(row.businessId);
        }).then(res => {
          this.getList();
          if(res.code == 200){
            this.msgSuccess("删除成功");

          }else{
            this.msgError(res.msg)
          }
        }).catch(function() {});
    }
  }
};
</script>
<style scoped>
  .app-container{
    margin: 20px;
    background-color: #fff;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }
</style>